package com.ztesoft.report.controller;


import com.ztesoft.report.IsPermission.annotion.isPermission;
import com.ztesoft.report.base.controller.BaseController;
import com.ztesoft.report.base.pojo.R;
import com.ztesoft.report.entity.ReportCategory;
import com.ztesoft.report.service.ReportCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 报表类别表 前端控制器
 * </p>
 *
 * @author y3tu
 * @since 2018-05-29
 */
@RestController
@RequestMapping("/reportCategory")
@Slf4j
public class ReportCategoryController extends BaseController {
    @Autowired
    ReportCategoryService reportCategoryService;
    //private Logger logger = LoggerFactory.getLogger(ReportCategory.class);

    @RequestMapping("/getTreeList")
    public Map getTreeList(@RequestBody Integer type) {
        Map map = null;
        try {
            map = reportCategoryService.getTreeList(type);
        } catch (Exception e) {
            log.error("", e);
            map = R.error("查询失败,服务器异常，请联系相关人员！");
        }
        return map;
    }

    @RequestMapping("/addTreeNode")
    public Map addTreeNode(@RequestBody ReportCategory reportCategory) {
        reportCategory.setCreateUser(getCurrentUser());
        Map map = null;
        try {
            map = reportCategoryService.addTreeNode(reportCategory);
        } catch (RuntimeException e) {
            log.error("", e);
            map = R.error("新增失败,新增节点存在与之相同父节点和名字的节点且生效，新增失败！");
        } catch (Exception e) {
            log.error("", e);
            map = R.error("新增失败,服务器异常，请联系相关人员！");
        }
        return map;
    }

    @RequestMapping("/delTreeNode")
    public Map delTreeNode(@RequestBody List<Integer> ids) {
        Map map = null;
        try {
            map = reportCategoryService.delTreeNode(ids, getCurrentUser());
        } catch (Exception e) {
            log.error("", e);
            map = R.error("删除失败,服务器异常，请联系相关人员！");
        }
        return map;
    }

    @RequestMapping("/updateTreeNode")
    public Map updateTreeNode(@RequestBody ReportCategory reportCategory) {
        reportCategory.setUpdateUser(getCurrentUser());
        Map map = null;
        try {
            map = reportCategoryService.updateTreeNode(reportCategory);
        } catch (Exception e) {
            log.error("", e);
            map = R.error("修改失败,服务器异常，请联系相关人员！");
        }
        return map;
    }

    @RequestMapping("/getReportList")
    public Map getReportList(@RequestBody Map<String, Object> param) {
        Map map = null;
        try {
            map = reportCategoryService.getReportList(param);
        } catch (Exception e) {
            log.error("", e);
            map = R.error("获取失败,服务器异常，请联系相关人员！");
        }
        return map;
    }

    @RequestMapping("/publishReport")
    //@isPermission(name="report:manger:publish")
    public Map publishReport(@RequestBody List<Map<String, Object>> param) {
        Map map = null;
        try {
            //第二个参数是合并发布判断开关
            map = reportCategoryService.publishReport(param,false);
        } catch (NullPointerException e) {
            map = R.error("发布失败，选择的报表包含未处理完成数据，不能再次发布！");
        } catch (ArrayIndexOutOfBoundsException e) {
            map = R.error("发布失败，选择的报表包含状态为禁用的数据，不能发布！");
        } catch (Exception e) {
            log.error("", e);
            map = R.error("发布失败,服务器异常，请联系相关人员！");
        }
        return map;
    }

    @RequestMapping("/publishMerge")
    public Map publishMerge(@RequestBody List<Map<String, Object>> param) {
        Map map = null;
        try {
            //第二个参数是合并发布判断开关
            map = reportCategoryService.publishReport(param,true);
        } catch (NullPointerException e) {
            map = R.error("发布失败，选择的报表包含未处理完成数据，不能再次发布！");
        } catch (ArrayIndexOutOfBoundsException e) {
            map = R.error("发布失败，选择的报表包含状态为禁用的数据，不能发布！");
        } catch (Exception e) {
            log.error("", e);
            map = R.error("发布失败,服务器异常，请联系相关人员！");
        }
        return map;
    }

    @RequestMapping("/delReport")
    //@isPermission(name = "reportMangerDelete")
    public Map delReport(@RequestBody List<Map<String, Object>> param) {
        Map map = null;
        try {
            map = reportCategoryService.delReport(param);
        } catch (NullPointerException e) {
            map = R.error("删除失败，选择的报表包含未处理完成数据，不能删除！");
        } catch (Exception e) {
            log.error("", e);
            map = R.error("删除失败,服务器异常，请联系相关人员！");
        }
        return map;
    }


    @RequestMapping("/test")
    public Map test() {
        Map map = null;
        try {
            map = reportCategoryService.test();
        } catch (Exception e) {
            log.error("", e);
            map = R.error("删除失败,服务器异常，请联系相关人员！");
        }
        return map;
    }
}
